<template>
  <div>
    <header class="custom-header">
      <div class="logo-container">
        <div class="logo">
          <img src="@/assets/icon/Logo.png" alt="Logo" class="icon" />
          <span class="site-name">红星党建</span>
        </div>
      </div>

      <nav class="custom-nav">
        <router-link
          v-for="item in menuItems"
          :key="item.path"
          :to="item.path"
          class="nav-item"
          active-class="active"
        >
          {{ item.name }}
        </router-link>
      </nav>

      <div class="avatar-container">
        <el-dropdown @command="handleCommand">
          <span class="avatar">
            <img src="@/assets/icon/Logo.png" alt="头像" class="avatar-icon" />
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="personal-center">个人中心</el-dropdown-item>
            <el-dropdown-item command="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </header>

    <main>
      <router-view />
    </main>
  </div>
</template>

<script>
import { ref, watch } from 'vue';
import { useRouter } from 'vue-router';

export default {
  name: "Layout",
  setup() {
    const isDropdownVisible = ref(false);
    const router = useRouter();

    const menuItems = [
      { name: '首页', path: '/user/home' },
      { name: '党建地点', path: '/user/location' },
      { name: '论坛', path: '/user/forum' },
      { name: '公告资讯', path: '/user/news' },
      { name: '商城', path: '/user/store' },
      { name: '个人中心', path: '/user/profile' }
    ];

    const handleCommand = (command) => {
      if (command === 'personal-center') {
        router.push('/personal-center');
      } else if (command === 'logout') {
        alert("退出登录");
        // 处理退出逻辑，例如清除 token
      }
    };

    return {
      isDropdownVisible,
      menuItems,
      handleCommand,
    };
  },
};
</script>

<style scoped>
.custom-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: red; 
  padding: 10px 20px;
}

.logo-container {
  display: flex; /* 确保 logo 和名称可以一起对齐 */
  align-items: center;
}

.logo {
  display: flex;
  align-items: center;
}

.icon {
  width: 40px;
  height: 40px;
  margin-right: 10px;
}

.site-name {
  font-size: 24px;
  font-weight: bold;
}

.custom-nav {
  display: flex;
  justify-content: center;
  flex: 1;
}

.nav-item {
  margin: 0 30px; /* 增大导航项间距 */
  text-decoration: none; /* 去掉默认下划线 */
  font-size: 20px; /* 字体大小 */
  color: white; /* 初始字体颜色 */
  position: relative; /* 用于下面的横线 */
  transition: color 0.3s, background-color 0.3s; /* 动画过渡 */
}

.nav-item:hover {
  color: yellow; /* 悬停时颜色 */
  background-color: rgba(255, 255, 0, 0.1); /* 悬停时背景颜色变浅 */
}

.nav-item.active {
  color: yellow; /* 选中时颜色 */
}

/* 选中状态下的横线，调整位置 */
.nav-item.active::after {
  content: '';
  display: block;
  height: 3px; /* 横线高度 */
  background-color: yellow; /* 横线的颜色 */
  position: absolute;
  left: 0;
  right: 0;
  bottom: -8px; /* 横线距离文本的距离 */
}

.avatar-container {
  display: flex;
  align-items: center; /* 确保头像与下拉菜单垂直对齐 */
}

.avatar-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
}
</style>
